﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BusinessComponents;
using BusinessComponents.Managers;
using System.Data;

namespace WaterfrontLodge.WebForms
{
    public partial class CustomerEdit : System.Web.UI.Page
    {
        string Strusername;
        Customer customer=new Customer();
        
       
      protected void Page_Load(object sender, EventArgs e)
        {
            
            if (!IsPostBack)
            {
                if (Page.User.IsInRole("admin"))
                {
                    if (Request.QueryString["id"] != null)
                    {
                        Strusername = Request.QueryString["id"];
                    }
                    else
                    {
                        Strusername = Page.User.Identity.Name;
                    }
                }
                else
                {
                    Strusername = Page.User.Identity.Name;
                }
                username.Text = Strusername;
                getdata();
            }
        }

        protected void Search_Click(object sender, EventArgs e)
        {

            getdata();
        }

        private void getdata()
        {
            customer = ManagerFactory.CustomerMgr.GetCustomer(Strusername);
            certifiNO.Text = customer.IDNO;
            telNO.Text = customer.TelephoneNO;
            addr.Text = customer.Address;
            if (customer.Gender.Trim() == "Male")
            {
                gender.SelectedIndex = 0;
            }
            else
            {
                gender.SelectedIndex = 1;
            }

            switch (customer.Salutation.Trim())
            {
                case "Student": career.SelectedIndex = 0;
                    break;
                case "Teacher": career.SelectedIndex = 1;
                    break;
                case "Engineer": career.SelectedIndex = 2;
                    break;
                case "Agent": career.SelectedIndex = 3;
                    break;
                case "Farmer": career.SelectedIndex = 4;
                    break;
                case "Worker": career.SelectedIndex = 5;
                    break;
                case "Doctor": career.SelectedIndex = 6;
                    break;
                case "Lawer": career.SelectedIndex = 7;
                    break;
                case "Officer": career.SelectedIndex = 8;
                    break;
                default:
                    career.SelectedIndex = 9;
                    break;

            }

            LblmesageCNO.Text = "";
            LblmessageTNO.Text = "";
            LblmessageGNO.Text = "";
            LblmessageCNO.Text = "";
            LblMessage.Text = "";

            ViewState["userid"] = customer.UserId;
        }

        protected void Edit_Click(object sender, EventArgs e)
        {
            if (Fieldvalidate())
            {
                customer.UserId = new Guid(ViewState["userid"].ToString());
                customer.UserName = username.Text.Trim();
                customer.IDNO = certifiNO.Text.Trim();
                customer.TelephoneNO = telNO.Text.Trim();
                customer.Address = addr.Text.Trim();
                customer.Gender = gender.SelectedValue;
                customer.Salutation = career.SelectedValue;

                ManagerFactory.CustomerMgr.UpdateCustomer(customer);

                LblmesageCNO.Text = "";
                LblmessageTNO.Text = "";
                LblmessageGNO.Text = "";
                LblmessageCNO.Text = "";

                LblMessage.Text = "Update successful!";
            }
            else
            {
                LblMessage.Text = "Please finish the requried content! ";
            }
        }

        protected Boolean Fieldvalidate()
        {
            Boolean b = true;
            if (this.certifiNO.Text.Trim() == "")
            {
                b = false;
                LblmesageCNO.Text = "Please enter Certification Number!";
            }
            else { LblmesageCNO.Text = ""; }
            if (this.telNO.Text.Trim() == "")
            {
                b = false;
                LblmessageTNO.Text = "Please enter Telephone Number!";
            }
            else { LblmessageTNO.Text = ""; }
            if (this.gender.SelectedValue.Trim() == "")
            {
                b = false;
                LblmessageGNO.Text = "Please enter Gender!";
            }
            else { LblmessageGNO.Text = ""; }
            if (this.career.SelectedValue.Trim() == "")
            {
                b = false;
                LblmessageCNO.Text = "Please enter Occupation!";
            }
            else { LblmessageCNO.Text = ""; }
            return b;
        }
    }
}